﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace shape{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)//计算圆面积的按钮
        {
            double r = double.Parse(textBox7.Text);
            Shape shape = new Circle(r);
            if (shape.IsLegal())
            {
                label8.Text = shape.GetArea().ToString();
            }
            else
            {
                label8.Text = "输入不合法";
            } 
        }
        private void button2_Click(object sender, EventArgs e)//计算矩形面积的按钮
        {
            double l = double.Parse(textBox3.Text);
            double w = double.Parse(textBox4.Text);
            Shape shape = new Rectangle(l,w);
            if (shape.IsLegal())
            {
                label9.Text = shape.GetArea().ToString();
            }
            else
            {
                label9.Text = "输入不合法";
            }
        }
        private void button3_Click(object sender, EventArgs e)//计算正方形面积的按钮
        {
            double a= double.Parse(textBox6.Text);
            Shape shape = new Square(a);
            if (shape.IsLegal())
            {
                label10.Text = shape.GetArea().ToString();
            }
            else
            {
                label10.Text = "输入不合法";
            }
        }
        private void button4_Click(object sender, EventArgs e)//随机生成十个图形并计算面积之和的按钮
        {
            Random rd = new Random();
            double area=0;
            for (int i = 0; i <3; i++)
            {
                int r = rd.Next(0, 20);
                Shape shape1=new Circle(r);
                area+=shape1.GetArea();
                int width = rd.Next(0, 20);
                int height = rd.Next(width,20);
                Shape shape2=new Rectangle(height,width);
                area += shape2.GetArea();
                int a=rd.Next(0, 20);
                Shape shape3=new Square(a);
                area += shape3.GetArea();
            }
            int b = rd.Next(0, 20);
            Shape shape10 = new Square(b);
            area += shape10.GetArea();
            label12.Text = area.ToString();

        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }
    }
    //抽象类
    public abstract class Shape
    {
        public abstract double GetArea();
        public abstract bool IsLegal();
    }
    //圆
    public class Circle : Shape
    {
        private double _r;
        public double R
        {
            get { return _r; }
            set { _r = value; }
        }
        public Circle(double r)
        {
            this.R = r;
        }
        public override double GetArea()
        {
            return Math.PI * R * R;
        }
        public override bool IsLegal()//是否合法
        {
            if (this.R < 0)
                return false;
            else
                return true;
        }
    }
    //矩形
    public class Rectangle : Shape
    {
        private double _length;
        public double Length
        {
            get { return _length; }
            set { _length = value; }
        }
        private double _width;
        public double width
        {
            get { return _width; }
            set { _width = value; }
        }
        public Rectangle(double length, double width)
        {
            this.Length = length;
            this.width = width;
        }
        public override double GetArea()
        {
            return Length * width;
        }
        public override bool IsLegal()//是否合法
        {
            if (this.Length < 0 || this.width < 0 || this.Length < this.width)
                return false;
            else
                return true;
        }
    }
    //正方形
    public class Square : Shape
    {
        private double _a;//边长
        public double A
        {
            get { return _a; }
            set { _a = value; }
        }
        public Square(double a)
        {
            this.A = a;
        }
        public override double GetArea()
        {
            return  A*A;
        }
        public override bool IsLegal()//是否合法
        {
            if (this.A < 0)
                return false;
            else
                return true;
        }
    }
    
    }

